import { Editor } from 'tinymce'
import { createBasePlugin } from '../base/index'
import { ossSaveMinioFileUsingPost } from '@/api/4a/osswenjianshangchuan'

const svg =
  '<svg viewBox="0 0 1024 1024" width="16" height="16"><path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm0 820c-205.3 0-372-166.7-372-372S306.7 140 512 140s372 166.7 372 372-166.7 372-372 372zm0-684c-172.2 0-312 139.8-312 312s139.8 312 312 312 312-139.8 312-312-139.8-312-312-312zm0 564c-139.7 0-252-112.3-252-252s112.3-252 252-252 252 112.3 252 252-112.3 252-252 252zm0-464c-116.6 0-212 95.4-212 212s95.4 212 212 212 212-95.4 212-212-95.4-212-212-212z"/></svg>'

const filePreviewPlugin = createBasePlugin({
  name: 'filePreview',
  svg,
  accept: '.doc,.docx,.xls,.xlsx',
  tooltip: '文件预览',
  handler: async (editor: Editor, file: File) => {
    if (!file) return
    // 上传文件
    const res = await ossSaveMinioFileUsingPost({ body: {}, file })
    if (res && res.code === 0 && res.data && res.data.path) {
      const previewUrl = `https://view.officeapps.live.com/op/view.aspx?src=${encodeURIComponent(res.data.path)}`
      editor.insertContent(
        `<iframe src='${previewUrl}' width='100%' height='600px' frameborder='0'></iframe>`
      )
    } else {
      editor.notificationManager.open({
        text: '文件上传失败',
        type: 'error'
      })
    }
  }
})

export default filePreviewPlugin
